<?php

namespace Admin\Controller;

/**
 * Created by wangchao.
 * User: Administrator
 * Date: 2017/11/10 0010
 * Time: 14:05
 */

class CheckController extends CommonController
{

//待办
    public function todo_list()
    {
        //$user_id = $_SESSION['user_id']; //用户登录
        $user_id = $_SESSION['admin_id']; //用户登录
        //查询当前用户待办数量

        $wherer['status']=1;
        $wherer['pending_user_id']=$user_id;
        $push_r = M('push_record'); //实例化Data数据模型
        $nums = $push_r->where($wherer)->count();
        //根据当前用户，查询该用户待办的危险源ID,（待办的危险源ID是指其下级负责的提交的），  PushRecord  pending_user_id= $user_id
        //根据危险源ID获得风险点ID，会存在多个待处理危险源，可能有同一个危险源多日排查有问题但均为处理的情况
       //sql    select id,  name from   xc_danger where id in
        //     (select  DISTINCT(danger_id)   from xc_object where  status=1 and object_id in
       //     (select DISTINCT object_id  from xc_push_record  where pending_user_id='22'))


        $admin_id = $_SESSION['admin_id'];//如果是管理员登录
        $level_id = $_SESSION['level_id']; //用户登录等级
        $object_ids = $_SESSION['object_ids'];//该用户负责的风险点

        //查询具有危险源为公司级的风险点id和名称
        $sql = "select id,  name from   xc_danger where id in (select  DISTINCT(danger_id)   from xc_object where  status=1 and object_id in
        (select DISTINCT object_id  from xc_push_record  where pending_user_id=".$user_id."))" ;

        $Data = M('danger'); //实例化Data数据模型
        $list = $Data->query($sql);
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                //根据风险点ID查询，查询该风险点下的待处理的哪些危险源ID和名称[以及该危险源的多少个待处理记录]
                $sql1="select object_id,danger_id,name   from xc_object where  status=1  and danger_id =".$v['id']." and  object_id  in(
                       select  DISTINCT object_id  from xc_push_record  where `status`=1 and  pending_user_id=". $user_id.")";

                $Data1 = M('object'); //实例化Data数据模型
                $res2 = $Data->query($sql1);
                $list[$k]['voo'] = $res2;
            }
        }


        $this->assign('nums', $nums);
        $this->assign('list', $list);
        $this->display('todo_list');

    }
    //待办-某危险源的待办类别
    public function todo_list_wxylist()
    {
         //get当前危险源，查询该危险源列表，点击某行，进行处理  object_id

        if (IS_GET) {
            //获取传过来的$object_id并且pending_user_id 为当前用户的数据
            $object_id = $_GET['object_id'];
            $user_id = $_SESSION['user_id']; //用户登录
            //写入session 备用
            session('object_id',  $object_id);
            $sql="select  *   from xc_push_record  where `status`=1 and  pending_user_id=".$user_id." and object_id=".$object_id;
            $Data1 = M('push_record');
            $list = $Data1->query($sql);
            $count=  0;
            //$count
         //   $list = $this->lists('PushRecord', $where, 'push_id desc');
            foreach ($list as $k => $v) {
                $list[$k]['push_user_name'] = M('User')->where(['user_id' => $v['push_user_id']])->getField('username');
                $list[$k]['deal_user_name'] = M('User')->where(['user_id' => $v['deal_user_id']])->getField('username');
                $list[$k]['pending_user_name'] = M('User')->where(['user_id' => $v['pending_user_id']])->getField('username');

                $list[$k]['object_name']  = M('Object')->where(['object_id' =>  $object_id ])->getField('name');


                //销毁以下指定的变量
                unset($list[$k]['push_user_id']);
                unset($list[$k]['deal_user_id']);
                unset($list[$k]['pending_user_id']);
                $list[$k]['img_url'] = C('ADDR').$v['img_url'];
                $sug1 = M('DealInfo')->where(['level_id' => 4, 'push_id' => $v['push_id']])->getField('suggest');
                $sug2 = M('DealInfo')->where(['level_id' => 3, 'push_id' => $v['push_id']])->getField('suggest');
                $sug3 = M('DealInfo')->where(['level_id' => 2, 'push_id' => $v['push_id']])->getField('suggest');
                $sug4 = M('DealInfo')->where(['level_id' => 1, 'push_id' => $v['push_id']])->getField('suggest');
                if (!empty($sug1)) {
                    $list[$k]['sug1'] = $sug1;
                } else {
                    $list[$k]['sug1'] = '';
                }
                if (!empty($sug2)) {
                    $list[$k]['sug2'] = $sug2;
                } else {
                    $list[$k]['sug2'] = '';
                }
                if (!empty($sug3)) {
                    $list[$k]['sug3'] = $sug3;
                } else {
                    $list[$k]['sug3'] = '';
                }
                if (!empty($sug4)) {
                    $list[$k]['sug4'] = $sug4;
                } else {
                    $list[$k]['sug4'] = '';
                }

                $count++;
            }

            $this->assign('list', $list);
            $this->assign('count', $count);

            $this->display('todo_list_wxylist');
        }
    }


    //待办详细页面 进行待办处理
    public function  todo_do()
    {
        $level_id = $_SESSION['level_id']; //用户登录等级
        $push_id = $_GET['push_id'];
        $user_id = $_SESSION['user_id']; //用户登录
        session('push_id',  $push_id);
        //打开详细页面根据危险源ID获取该危险源详细信息
        if (IS_GET) {
            //push_id 根据ID查询该记录对应的危险源、风险点信息


            //1、首先查询出危险源ID，根据ID再查询危险源的信息，危险源名称、风险等级、管控措施、管控层级 、排查周期、责任人
            $push_info = M('push_record')->where(['push_id' => $push_id])->find();
            $object_id=$push_info['object_id'];
  //echo $object_id;
            $object_info = M('Object')->where(['object_id' => $object_id])->find();
          //  $object_info['name']    danger_level_id 风险等级   danger_source (管控措施)  object_level_id 管控层级  engineering_method  隐患列表
          //管控层级根据数字对应五个等级 1为公司级、2为专业级、3为车间级、4为班组级 ，排查周期根据管控层级数对应 日、周、旬、月
          //根据危险源ID查询责任人 ，每个风险点可能存在多个责任人，比如员工级、班组级、车间级  user表 object_ids
          //   SELECT level_id,username,object_ids  FROM xc_user  where object_ids like  "%100%" or object_ids like  "%100,%"  ORDER BY level_id desc
            $sql='SELECT level_id,username,object_ids  FROM xc_user  where object_ids like  "%'. $object_id.'%" or object_ids like  "%'.$object_id.',%"  ORDER BY level_id desc';
            $zerenrens= M('user')->query($sql);
            //echo  $zerenrens[1]['username'];
           //2、查询风险点名称
            $where['object_id']=$object_id;
            $where['status']=1;
            $danger = M('danger')->where($where)->find();

 //显示及填写处理意见的界面控制，如果数据库里已有相关字段不为空且不属于当前等级则为不可写状态，如果为当前等级则可进行编辑。
            $sug1 = M('DealInfo')->where(['level_id' => 4, 'push_id' => $push_id])->getField('suggest');
            $sug2 = M('DealInfo')->where(['level_id' => 3, 'push_id' => $push_id])->getField('suggest');
            $sug3 = M('DealInfo')->where(['level_id' => 2, 'push_id' => $push_id])->getField('suggest');
            $sug4 = M('DealInfo')->where(['level_id' => 1, 'push_id' => $push_id])->getField('suggest');

            $this->assign('sug1',$sug1);
            $this->assign('sug2',$sug2);
            $this->assign('sug3',$sug3);
            $this->assign('sug4',$sug4);

            $this->assign('level_id',$level_id);//用于前台判断该等级可操作的对应等级框不能填写、修改其他等级的内容

            $this->assign('zerenrens',$zerenrens);
            $this->assign('danger',$danger);
            $this->assign('push_info', $push_info);
            $this->assign('object_info', $object_info);
            $this->display('todo_do');
        }
        if (IS_POST) {
            $is_agree=1;
            $suggest='';
            //提交的是哪一个文本框呢？根据当前登录等级来判断
          if($level_id ==1){ $suggest =I('post.deal5');}
          if($level_id ==2){ $suggest =I('post.deal4');}
          if($level_id ==3){ $suggest =I('post.deal3');}
          if($level_id ==4){ $suggest =I('post.deal2');}
            if(isset($_POST['guidang']))
            {
                $is_agree=3;
            }
            if(isset($_POST['tijiao']))
            {
                $is_agree=1;
            }
            if(isset($_POST['bohui']))
            {
                $is_agree=0;
            }
            //NO.1 判断提交的是哪个按钮提交，【提交】 is_agree=1，【驳回】  is_agree=0，【归档】 is_agree=3 写入dealinfo表 。
            //？？？？？提交则代表流程未结束还需要进行下一步处理，is_agree=0
            try {
                $arr = [
                    'push_id' => $push_id,
                    'deal_user_id' => $user_id,
                    'level_id' => $level_id,
                    'suggest' => $suggest,
                    'create_time' => date('Y-m-d H:i:s', time()),
                    'is_agree' => $is_agree,
                ];
            /*    if (isset($post['id'])) {
                    $res = M('DealInfo')->where(['id' => $post['id']])->save($arr);
                } else {*/
                    $res = M('DealInfo')->add($arr);
             //   }
                if (empty($res)) {
                    throw new Exception('添加处理意见失败！');
                }
                //如果同意，status为1，最高领导为2即为归档，新需求车间级也可归档，不同意即is_agree为0则为驳回，修改状态
             //   //1、提交下一步，status=1,更新下一步处理人，当前处理人;如果已经是最高级则状态为2归档
                if ($is_agree == 1) {
                    $parent_id = M('User')->where(['user_id' => $user_id])->getField('parent_id');
                    $data = [
                        'deal_user_id' => $user_id,
                        'pending_user_id' => $parent_id,
                    ];
                    // 是否是最高领导审核
                    if ($level_id == 1) {
                        $data['status'] = 2;
                    } else {
                        $data['status'] = 1;
                    }
                    M('PushRecord')->where(['push_id' => $push_id])->save($data);
                    // 推送
                    $j_push_id = M('User')->where(['user_id' => $parent_id])->getField('j_push_id');
                    if (!empty($j_push_id)) {
                        jPushs('您有新的事件待处理', 1, $j_push_id, $user_id);
                    }
                  //  $phone = M('User')->where(['user_id' => $parent_id])->getField('phone');
                    $phone ='18669955112';
                    if (!empty($phone)) {
                        $this->http_send_post($phone);
                    }
                } elseif ($is_agree == 0) {
                    //2、驳回，状态status为0，更新下一步处理人，当前处理人
                    $deal_user_id = M('PushRecord')->where(['push_id' => $push_id])->getField('deal_user_id');
                    $data = [
                        'status' => 0,
                        'deal_user_id' => $user_id,
                        'pending_user_id' => $deal_user_id,
                    ];
                    M('PushRecord')->where(['push_id' => $push_id])->save($data);
                    // 推送
                    $j_push_id = M('User')->where(['user_id' => $deal_user_id])->getField('j_push_id');
                    if (!empty($j_push_id)) {
                        jPushs('您有事件被驳回！', 2, $j_push_id, $user_id);
                    }
                   // $phone = M('User')->where(['user_id' => $deal_user_id])->getField('phone');
                    $phone ='18669955112';
                    if (!empty($phone)) {
                        $this->http_send_post($phone);
                    }
                } else {
                    //3、归档 默认is_agree=3，更新 状态status为2 当前处理人，上级处理人为0；
                    $data = [
                        'status' => 2,
                        'deal_user_id' => $user_id,
                        'pending_user_id' => 0,
                    ];
                    M('PushRecord')->where(['push_id' => $push_id])->save($data);
                }
                M()->commit();
                $this->success('提交成功！');
              //  redirect(U('Admin/Check/todo_list_wxylist'));

            } catch (Exception $e) {
                M()->rollback();
                $this->error('提交失败！','' );
               // redirect(U('Admin/Check/todo_list_wxylist'));
            }

        }
    }
//查看照片
    public function  todo_list_pic()
    {
       // if (IS_GET) {
            $push_id = $_GET['push_id'];
            //根据push_id查询出所有图片 共四张
            $push_info = M('push_record')->where(['push_id' => $push_id])->find();
          //  $img_url=$push_info['img_url'];
            $this->assign('push_info',   $push_info);
            $this->display('todo_list_pic');
    //    }
    }
//查看视频
    public function  todo_list_video()
    {
        if (IS_GET) {
            $push_id = $_GET['push_id'];
            //根据push_id查询出视频信息共四张
            $push_info = M('push_record')->where(['push_id' => $push_id])->find();
            $this->assign('push_info',   $push_info);
            $this->display('todo_list_video');
        }
    }

    /**
     * 发送短信
     * @param $phone string 手机号
     * @param $msg string 内容
     * @return string
     */
    public function http_send_post($phone, $msg = '您收到一条待办通知，请及时登录安监平台APP处理。')
    {
        $url = 'http://yxt.wxtxsms.cn:8181/dx/sendPostExt.do';
        $post_data['uid']       = 'lylt';                  //用户ID
        $post_data['upsd']      = strtolower(md5('lylt@888')) ;   //密码
        $post_data['sendtele']  = $phone;                       //发送号码
        $post_data['msg']       = $msg;                //短信内容
        $post_data['sign']      = '星澜网络';                     //签名
        $post_data['ext'] = '';

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
        return request_post($url, $post_data);
    }

    //已办
    public function finish_list()
    {
        //$user_id = $_SESSION['user_id']; //用户登录
        $user_id = 44; //用户登录
        $sql="select  r.*,d.id,d.push_id,d.suggest   from xc_push_record  r,xc_deal_info d where  r.push_id= d.push_id  and    d.deal_user_id=".$user_id ." ORDER BY d.id desc ";
        echo $sql;exit;
        $Data1 = M('push_record');
        $list = $Data1->query($sql);
        $count=  0;
        //$count
        //   $list = $this->lists('PushRecord', $where, 'push_id desc');
        foreach ($list as $k => $v) {
            $list[$k]['push_user_name'] = M('User')->where(['user_id' => $v['push_user_id']])->getField('username');
            $list[$k]['deal_user_name'] = M('User')->where(['user_id' => $v['deal_user_id']])->getField('username');
            $list[$k]['pending_user_name'] = M('User')->where(['user_id' => $v['pending_user_id']])->getField('username');

            $list[$k]['object_name']  = M('Object')->where(['object_id' =>  $v['object_id']  ])->getField('name');


            //销毁以下指定的变量
            unset($list[$k]['push_user_id']);
            unset($list[$k]['deal_user_id']);
            unset($list[$k]['pending_user_id']);
            $list[$k]['img_url'] = C('ADDR').$v['img_url'];
            $sug1 = M('DealInfo')->where(['level_id' => 4, 'push_id' => $v['push_id']])->getField('suggest');
            $sug2 = M('DealInfo')->where(['level_id' => 3, 'push_id' => $v['push_id']])->getField('suggest');
            $sug3 = M('DealInfo')->where(['level_id' => 2, 'push_id' => $v['push_id']])->getField('suggest');
            $sug4 = M('DealInfo')->where(['level_id' => 1, 'push_id' => $v['push_id']])->getField('suggest');
            if (!empty($sug1)) {
                $list[$k]['sug1'] = $sug1;
            } else {
                $list[$k]['sug1'] = '';
            }
            if (!empty($sug2)) {
                $list[$k]['sug2'] = $sug2;
            } else {
                $list[$k]['sug2'] = '';
            }
            if (!empty($sug3)) {
                $list[$k]['sug3'] = $sug3;
            } else {
                $list[$k]['sug3'] = '';
            }
            if (!empty($sug4)) {
                $list[$k]['sug4'] = $sug4;
            } else {
                $list[$k]['sug4'] = '';
            }

            $count++;
        }

        $this->assign('list', $list);
        $this->assign('count', $count);

        $this->display('finish_list');


    }
    public function finish_view()
      {
          //查看已处理的详细页面
          $level_id = $_SESSION['level_id']; //用户登录等级
          $push_id = $_GET['push_id'];
          $user_id = $_SESSION['user_id']; //用户登录
          session('push_id',  $push_id);
          //打开详细页面根据危险源ID获取该危险源详细信息
          if (IS_GET) {
              //push_id 根据ID查询该记录对应的危险源、风险点信息

              //1、首先查询出危险源ID，根据ID再查询危险源的信息，危险源名称、风险等级、管控措施、管控层级 、排查周期、责任人
              $push_info = M('push_record')->where(['push_id' => $push_id])->find();
              $object_id=$push_info['object_id'];
              //echo $object_id;
              $object_info = M('Object')->where(['object_id' => $object_id])->find();
              //  $object_info['name']    danger_level_id 风险等级   danger_source (管控措施)  object_level_id 管控层级  engineering_method  隐患列表
              //管控层级根据数字对应五个等级 1为公司级、2为专业级、3为车间级、4为班组级 ，排查周期根据管控层级数对应 日、周、旬、月
              //根据危险源ID查询责任人 ，每个风险点可能存在多个责任人，比如员工级、班组级、车间级  user表 object_ids
              //   SELECT level_id,username,object_ids  FROM xc_user  where object_ids like  "%100%" or object_ids like  "%100,%"  ORDER BY level_id desc
              $sql='SELECT level_id,username,object_ids  FROM xc_user  where object_ids like  "%'. $object_id.'%" or object_ids like  "%'.$object_id.',%"  ORDER BY level_id desc';
              $zerenrens= M('user')->query($sql);
              //echo  $zerenrens[1]['username'];
              //2、查询风险点名称
              $where['object_id']=$object_id;
              $where['status']=1;
              $danger = M('danger')->where($where)->find();

              //显示及填写处理意见的界面控制，如果数据库里已有相关字段不为空且不属于当前等级则为不可写状态，如果为当前等级则可进行编辑。
              $sug1 = M('DealInfo')->where(['level_id' => 4, 'push_id' => $push_id])->getField('suggest');
              $sug2 = M('DealInfo')->where(['level_id' => 3, 'push_id' => $push_id])->getField('suggest');
              $sug3 = M('DealInfo')->where(['level_id' => 2, 'push_id' => $push_id])->getField('suggest');
              $sug4 = M('DealInfo')->where(['level_id' => 1, 'push_id' => $push_id])->getField('suggest');

              $this->assign('sug1',$sug1);
              $this->assign('sug2',$sug2);
              $this->assign('sug3',$sug3);
              $this->assign('sug4',$sug4);

              $this->assign('level_id',$level_id);//用于前台判断该等级可操作的对应等级框不能填写、修改其他等级的内容

              $this->assign('zerenrens',$zerenrens);
              $this->assign('danger',$danger);
              $this->assign('push_info', $push_info);
              $this->assign('object_info', $object_info);
              $this->display('finish_view');
          }


      }

}